package com.zhuoshi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zhuoshi.entity.Project;
import com.zhuoshi.entity.Unit;
import com.zhuoshi.entity.UnitCustom;
import com.zhuoshi.service.ProjectService;
import com.zhuoshi.service.UnitService;

@Controller
@RequestMapping("unit")
public class UnitController {
	@Autowired
	private UnitService unitService;
	
	@Autowired
	private ProjectService projectService;
	
	//通过楼栋id查询所有单元
	@RequestMapping("selectUnitByComplexIdLiu")
	public String selectUnitByComplexIdLiu(HttpServletResponse response,Integer complexid){
		List<Unit> units=unitService.selectUnitByComplexIdLiu(complexid);
		JSONObject json=new JSONObject();
		json.put("units", units);
		PrintWriter out=null;
		try {
			out=response.getWriter();
			out.print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//查询全部单元
	@RequestMapping("selectAllUnitLiu")
	public ModelAndView selectAllUnitLiu(String unumber) {
		List<UnitCustom> units=unitService.selectAllUnitLiu(unumber);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("units", units);
		modelAndView.setViewName("/Admin/unitManager.jsp");
		return modelAndView;
	}
	
	//添加单元信息
	@RequestMapping("addUnitMessageLiu")
	public ModelAndView addUnitMessageLiu() throws Exception {
		List<Project> projects=projectService.selectAllProjectLiu();
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("projects", projects);
		modelAndView.setViewName("/Admin/addUnit.jsp");
		return modelAndView;
	}
	
	//确认添加
	@RequestMapping("addUnitConfirmLiu")
	public String addUnitConfirmLiu(Unit unit) {
		int rows=unitService.addUnitConfirmLiu(unit);
		if(rows>0) {
			return "redirect:selectAllUnitLiu.action";
		}else {
			return "redirect:error.jsp";
		}
	}
	
	//删除单元
	@RequestMapping("deleteUnitById")
	public String deleteUnitById(String id) {
		String []ids=id.split(",");
		int a=0;
		for(int i=0;i<ids.length;i++) {
			int rows=unitService.deleteUnitById(Integer.parseInt(ids[i]));
			a+=rows;
		}		
		if(a==ids.length) {
			return "redirect:selectAllUnitLiu.action";
		}else {
			return "redirect:error.jsp";
		}
	}
	
	//修改单元信息
	@RequestMapping("updateUnitMessageLiu")
	public ModelAndView updateUnitMessageLiu(int id) throws Exception {
		UnitCustom unit=unitService.selectUnitCustomById(id);
		List<Project> projects=projectService.selectAllProjectLiu();
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("projects", projects);
		modelAndView.addObject("unit", unit);
		modelAndView.setViewName("/Admin/updateUnit.jsp");
		return modelAndView;
	}
	
	//确认修改信息
	@RequestMapping("updateUnitConfirmLiu")
	public String updateUnitConfirmLiu(Unit unit) {
		int rows=unitService.updateUnitConfirmLiu(unit);
		if(rows>0) {
			return "redirect:selectAllUnitLiu.action";
		}else {
			return "redirect:error.jsp";
		}
	}
}
